
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>最强日期正则表达式 - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,简单的日期判断（YYYY/MM/DD）：1^\d&amp;#123;4&amp;#125;(\-|\/|\.)\d&amp;#123;1,2&amp;#125;\1\d&amp;#123;1,2&amp;#125;$
演化的日期判断（YYYY/M,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">最强日期正则表达式</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Js"><b>「
                    </b>JS<b> 」</b></a>
                
                April 14, 2018
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2018/04/14/Js/%E6%9C%80%E5%BC%BA%E6%97%A5%E6%9C%9F%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="最强日期正则表达式" class="">最强日期正则表达式</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    21k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    19 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/%E6%AD%A3%E5%88%99/" rel="tag">正则</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h2 id="简单的日期判断（YYYY-MM-DD）："><a href="#简单的日期判断（YYYY-MM-DD）：" class="headerlink" title="简单的日期判断（YYYY/MM/DD）："></a>简单的日期判断（YYYY/MM/DD）：</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^\d&#123;<span class="number">4</span>&#125;(\-|\/|\.)\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;\<span class="number">1</span>\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;$</span><br></pre></td></tr></table></figure>
<h2 id="演化的日期判断（YYYY-MM-DD-YY-MM-DD）"><a href="#演化的日期判断（YYYY-MM-DD-YY-MM-DD）" class="headerlink" title="演化的日期判断（YYYY/MM/DD| YY/MM/DD）"></a>演化的日期判断（YYYY/MM/DD| YY/MM/DD）</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^(^(\d&#123;<span class="number">4</span>&#125;|\d&#123;<span class="number">2</span>&#125;)(\-|\/|\.)\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;\<span class="number">3</span>\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;$)|(^\d&#123;<span class="number">4</span>&#125;年\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;月\d&#123;<span class="number">1</span>,<span class="number">2</span>&#125;日$)$</span><br></pre></td></tr></table></figure>
<h2 id="加入闰年的判断的："><a href="#加入闰年的判断的：" class="headerlink" title="加入闰年的判断的："></a>加入闰年的判断的：</h2><p>实例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^((((<span class="number">1</span>[<span class="number">6</span><span class="number">-9</span>]|[<span class="number">2</span><span class="number">-9</span>]\d)\d&#123;<span class="number">2</span>&#125;)-(<span class="number">0</span>?[<span class="number">13578</span>]|<span class="number">1</span>[<span class="number">02</span>])-(<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>]\d|<span class="number">3</span>[<span class="number">01</span>]))|(((<span class="number">1</span>[<span class="number">6</span><span class="number">-9</span>]|[<span class="number">2</span><span class="number">-9</span>]\d)\d&#123;<span class="number">2</span>&#125;)-(<span class="number">0</span>?[<span class="number">13456789</span>]|<span class="number">1</span>[<span class="number">012</span>])-(<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>]\d|<span class="number">30</span>))|(((<span class="number">1</span>[<span class="number">6</span><span class="number">-9</span>]|[<span class="number">2</span><span class="number">-9</span>]\d)\d&#123;<span class="number">2</span>&#125;)<span class="number">-0</span>?<span class="number">2</span>-(<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>]|<span class="number">1</span>\d|<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>]))|(((<span class="number">1</span>[<span class="number">6</span><span class="number">-9</span>]|[<span class="number">2</span><span class="number">-9</span>]\d)(<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">13579</span>][<span class="number">26</span>])|((<span class="number">16</span>|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">3579</span>][<span class="number">26</span>])<span class="number">00</span>))<span class="number">-0</span>?<span class="number">2</span><span class="number">-29</span>-))$</span><br></pre></td></tr></table></figure>
<p>分析：<br>1、什么是合法的日期范围？对于不同的应用场景，这个问题有不同的解释。这里采纳MSDN中的约定：<br>DateTime值类型表示值范围在公元（基督纪元）0001 年 1 月 1 日午夜 12:00:00 到公元 (C.E.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间。</p>
<p>2、关于闰年的阐释。<br>关于公历闰年是这样规定的：地球绕太阳公转一周叫做一回归年，一回归年长365日5时48分 46秒。因此，公历规定有平年和闰年，平年一年有365日，比回归年短0.2422日，四年共短0.9688日，故每四年增加一日，这一年有366日，就是闰年。但四年增加一日比四个回归年又多0.0312日,400年后将多3.12日,故在400年中少设3个闰年,也就是在400年中只设97个闰年，这样公历年的平均长度与回归年就相近似了。由此规定：年份是整百数的必须是400的倍数才是闰年，例如1900年、2100年就不是闰年。<br> <a id="more"></a></p>
<p>首先需要验证年份，显然，年份范围为 0001 - 9999，匹配YYYY的正则表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;|[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;</span><br></pre></td></tr></table></figure>
<p>其中 [0-9] 也可以表示为 \d，但 \d 不如 [0-9] 直观，因此下面我将一直采用 [0-9]</p>
<p>用正则表达式验证日期的难点有二：一是大小月份的天数不同，二是闰年的考虑。</p>
<blockquote>
<p>对于第一个难点，我们首先不考虑闰年，假设2月份都是28天，这样，月份和日期可以分成三种情况：<br>1、月份为 1, 3, 5, 7, 8, 10, 12，天数范围为 01 - 31，匹配MM-DD的正则表达式为：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">0</span>[<span class="number">13578</span>]|<span class="number">1</span>[<span class="number">02</span>])-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">3</span>[<span class="number">01</span>])</span><br></pre></td></tr></table></figure>
<p>2、月份为 4, 6, 9, 11，天数范围为 01-30，匹配MM-DD的正则表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">0</span>[<span class="number">469</span>]|<span class="number">11</span>)-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">30</span>)</span><br></pre></td></tr></table></figure>
<p>3、月份为 2，考虑平年情况，匹配MM-DD的正则表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">02</span>-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">1</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>])</span><br></pre></td></tr></table></figure>
<p>根据上面的成果，我们可以得到匹配平年日期格式为YYYY-MM-DD的正则表达式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;|[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;)-(((<span class="number">0</span>[<span class="number">13578</span>]|<span class="number">1</span>[<span class="number">02</span>])-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">3</span>[<span class="number">01</span>]))|((<span class="number">0</span>[<span class="number">469</span>]|<span class="number">11</span>)-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">30</span>))|(<span class="number">02</span>-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">1</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>])))</span><br></pre></td></tr></table></figure>
<blockquote>
<p>接着我们来解决第二个难点：闰年的考虑。根据闰年的定义，我们可以将闰年分为两类：</p>
</blockquote>
<p>1、能被4整除但不能被100整除的年份。寻找后两位的变化规律，可以很快得到下面的正则匹配：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;)(<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">13579</span>][<span class="number">26</span>])</span><br></pre></td></tr></table></figure>
<p>2、能被400整除的年份。能被400整除的数肯定能被100整除，因此后两位肯定是00，我们只要保证前两位能被4整除即可，相应的正则表达式为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(0[48]|[2468][048]|[3579][26])00</span><br></pre></td></tr></table></figure>
<p>2.最强验证日期的正则表达式,添加了闰年的验证</p>
<p>这个日期正则表达式支持<br>YYYY-MM-DD<br>YYYY/MM/DD<br>YYYY_MM_DD<br>YYYY.MM.DD的形式</p>
<p>match : 2008-2-29 2008/02/29</p>
<p>not match : 2008-2-30   2007-2-29</p>
<h2 id="完整的正则表达式如下"><a href="#完整的正则表达式如下" class="headerlink" title="完整的正则表达式如下"></a>完整的正则表达式如下</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">((^((<span class="number">1</span>[<span class="number">8</span><span class="number">-9</span>]\d&#123;<span class="number">2</span>&#125;)|([<span class="number">2</span><span class="number">-9</span>]\d&#123;<span class="number">3</span>&#125;))([-\/\._])(<span class="number">10</span>|<span class="number">12</span>|<span class="number">0</span>?[<span class="number">13578</span>])([-\/\._])(<span class="number">3</span>[<span class="number">01</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>])$)|(^((<span class="number">1</span>[<span class="number">8</span><span class="number">-9</span>]\d&#123;<span class="number">2</span>&#125;)|([<span class="number">2</span><span class="number">-9</span>]\d&#123;<span class="number">3</span>&#125;))([-\/\._])(<span class="number">11</span>|<span class="number">0</span>?[<span class="number">469</span>])([-\/\._])(<span class="number">30</span>|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>])$)|(^((<span class="number">1</span>[<span class="number">8</span><span class="number">-9</span>]\d&#123;<span class="number">2</span>&#125;)|([<span class="number">2</span><span class="number">-9</span>]\d&#123;<span class="number">3</span>&#125;))([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>]|<span class="number">1</span>[<span class="number">0</span><span class="number">-9</span>]|<span class="number">0</span>?[<span class="number">1</span><span class="number">-9</span>])$)|(^([<span class="number">2468</span>][<span class="number">048</span>]<span class="number">00</span>)([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">3579</span>][<span class="number">26</span>]<span class="number">00</span>)([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">1</span>][<span class="number">89</span>][<span class="number">0</span>][<span class="number">48</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">2</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>][<span class="number">0</span>][<span class="number">48</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">1</span>][<span class="number">89</span>][<span class="number">2468</span>][<span class="number">048</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">2</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>][<span class="number">2468</span>][<span class="number">048</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">1</span>][<span class="number">89</span>][<span class="number">13579</span>][<span class="number">26</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$)|(^([<span class="number">2</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>][<span class="number">13579</span>][<span class="number">26</span>])([-\/\._])(<span class="number">0</span>?<span class="number">2</span>)([-\/\._])(<span class="number">29</span>)$))</span><br></pre></td></tr></table></figure>
<p>闰年的2月份有29天，因此匹配闰年日期格式为YYYY-MM-DD的正则表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;)(<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">13579</span>][<span class="number">26</span>])|((<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">3579</span>][<span class="number">26</span>])<span class="number">00</span>))<span class="number">-02</span><span class="number">-29</span></span><br></pre></td></tr></table></figure>
<p>最后，将平年和闰年的日期验证表达式合并，我们得到最终的验证日期格式为YYYY-MM-DD的正则表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;|[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;)-(((<span class="number">0</span>[<span class="number">13578</span>]|<span class="number">1</span>[<span class="number">02</span>])-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">3</span>[<span class="number">01</span>]))|((<span class="number">0</span>[<span class="number">469</span>]|<span class="number">11</span>)-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">30</span>))|(<span class="number">02</span>-(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">1</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>]))))|((([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;)(<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">13579</span>][<span class="number">26</span>])|((<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">3579</span>][<span class="number">26</span>])<span class="number">00</span>))<span class="number">-02</span><span class="number">-29</span>)</span><br></pre></td></tr></table></figure>
<p>DD/MM/YYYY格式的正则验证表达式为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(((<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">3</span>[<span class="number">01</span>])/((<span class="number">0</span>[<span class="number">13578</span>]|<span class="number">1</span>[<span class="number">02</span>]))|((<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">12</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">30</span>)/(<span class="number">0</span>[<span class="number">469</span>]|<span class="number">11</span>))|(<span class="number">0</span>[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">1</span>][<span class="number">0</span><span class="number">-9</span>]|<span class="number">2</span>[<span class="number">0</span><span class="number">-8</span>])/(<span class="number">02</span>))/([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;[<span class="number">1</span><span class="number">-9</span>]|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;|[<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">1</span>&#125;[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;|[<span class="number">1</span><span class="number">-9</span>][<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">3</span>&#125;))|(<span class="number">29</span>/<span class="number">02</span>/(([<span class="number">0</span><span class="number">-9</span>]&#123;<span class="number">2</span>&#125;)(<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">13579</span>][<span class="number">26</span>])|((<span class="number">0</span>[<span class="number">48</span>]|[<span class="number">2468</span>][<span class="number">048</span>]|[<span class="number">3579</span>][<span class="number">26</span>])<span class="number">00</span>)))</span><br></pre></td></tr></table></figure>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#简单的日期判断（YYYY-MM-DD）："><span class="toc-number">1.</span> <span class="toc-text">简单的日期判断（YYYY&#x2F;MM&#x2F;DD）：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#演化的日期判断（YYYY-MM-DD-YY-MM-DD）"><span class="toc-number">2.</span> <span class="toc-text">演化的日期判断（YYYY&#x2F;MM&#x2F;DD| YY&#x2F;MM&#x2F;DD）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#加入闰年的判断的："><span class="toc-number">3.</span> <span class="toc-text">加入闰年的判断的：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#完整的正则表达式如下"><span class="toc-number">4.</span> <span class="toc-text">完整的正则表达式如下</span></a></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
